//
//  GZViewController.m
//  项目A
//
//  Created by lanou3g on 15/9/7.
//  Copyright (c) 2015年 王钧. All rights reserved.
//

#import "GZViewController.h"
#import "GZTableViewCell.h"
#import "CoreDataManager.h"
#import "UIImageView+WebCache.h"
#import "DPXQViewController.h"
#import "DPSingleton.h"
@interface GZViewController ()
@property (nonatomic, strong) NSMutableArray * GZarray;
@property (weak, nonatomic) IBOutlet UITableView *tabelView;
@end

@implementation GZViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我关注的单品";
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithTitle:@"清除" style:UIBarButtonItemStyleDone target:self action:@selector(FXAction)];
    self.navigationItem.rightBarButtonItem = left;
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    self.GZarray = [[NSMutableArray alloc] initWithArray:[[CoreDataManager defaultManager] fetchManagedObjectModelWithName:@"EntityModel" predicate:nil sortKeys:nil]];
}

- (void) FXAction
{
    UIAlertView *alret = [[UIAlertView alloc]initWithTitle:@"提示" message:@"清空收藏成功！"delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alret show];
 
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        [[CoreDataManager defaultManager] deleteAllManagedObjectModels:self.GZarray];
        // 将数组中所有的对象删除
        [self.GZarray removeAllObjects];
    }
    [self.tabelView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.GZarray.count;
}

- (GZTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GZTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GZcell" forIndexPath:indexPath];
    EntityModel * model = self.GZarray[indexPath.row];
    [cell.GZImageView sd_setImageWithURL:[NSURL URLWithString:model.imageurl] placeholderImage:[UIImage imageNamed:@"iconfont-icontu"]];
    cell.TitelView.text = model.titel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EntityModel * model = self.GZarray[indexPath.row];
    DPXQViewController * xqvc = [[DPXQViewController alloc]init];
    [self.navigationController pushViewController:xqvc animated:YES];
    xqvc.urlImage = model.imageurl;
    xqvc.titelString = model.titel;
    xqvc.descriptionString = model.descriptions;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
